package com.mgy.common.excel;

import java.util.*;

/**
 * 列集合
 *
 * @author maguoyong
 * @since 2018/01/24
 */
public class DataColumnCollection implements List<DataColumn> {
    /**
     * 存放DataColumn对象
     */
    private List<DataColumn> columns;

    private DataTable dataTable;

    /**
     * 创建一个新的实例 DataColumnCollection.
     */
    public DataColumnCollection(DataTable dataTable) {
        this.columns = new Vector<>();
        this.dataTable = dataTable;
    }


    /**
     * 返回DataTable的实际列数
     */
    @Override
    public int size() {
        return this.columns.size();
    }

    /**
     * 清空columns和nameMap
     */
    @Override
    public void clear() {
        this.columns.clear();
    }

    /**
     * 给columns加入一列
     */
    @Override
    public boolean add(DataColumn column) {
        column.setOrdinal(this.columns.size());
        return this.columns.add(column);


    }

    /**
     * 在指定位置 给columns加入一列
     */
    @Override
    public void add(int index, DataColumn column) {
        this.columns.add(index, column);
        column.setOrdinal(index);
        for (int i = index + 1; i < this.columns.size(); i++) {
            DataColumn dataColumn = this.columns.get(i);
            dataColumn.setOrdinal(dataColumn.getOrdinal() + 1);
            this.columns.set(i, dataColumn);
        }
    }


    /**
     * 删除columns的一列数据
     */
    public void remove(DataColumn column) {
        this.columns.remove(column);
    }

    /**
     * 删除columns的指定列数据
     */
    @Override
    public DataColumn remove(int index) {
        return this.columns.remove(index);

    }

    /**
     * 得到指定列位置数据
     */
    @Override
    public DataColumn get(int index) {
        return this.columns.get(index);
    }

    @Override
    public boolean isEmpty() {
        return this.columns.isEmpty();
    }

    @Override
    public boolean contains(Object o) {
        return this.columns.contains(o);
    }

    @Override
    public Iterator<DataColumn> iterator() {
        return this.columns.iterator();
    }

    @Override
    public Object[] toArray() {
        return this.columns.toArray();
    }

    @Override
    public <T> T[] toArray(T[] a) {
        return this.columns.toArray(a);
    }

    @Override
    public boolean remove(Object o) {
        return this.columns.remove(o);
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return this.columns.containsAll(c);
    }

    @Override
    public boolean addAll(Collection<? extends DataColumn> c) {
        return this.columns.addAll(c);
    }

    @Override
    public boolean addAll(int index, Collection<? extends DataColumn> c) {
        return this.columns.addAll(index, c);
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return this.columns.removeAll(c);
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return this.columns.retainAll(c);
    }

    @Override
    public DataColumn set(int index, DataColumn element) {
        return this.columns.set(index, element);
    }

    @Override
    public int indexOf(Object o) {
        return this.columns.indexOf(o);
    }

    @Override
    public int lastIndexOf(Object o) {
        return this.columns.lastIndexOf(o);
    }

    @Override
    public ListIterator<DataColumn> listIterator() {
        return this.columns.listIterator();
    }

    @Override
    public ListIterator<DataColumn> listIterator(int index) {
        return this.columns.listIterator(index);
    }

    @Override
    public List<DataColumn> subList(int fromIndex, int toIndex) {
        return this.columns.subList(fromIndex, toIndex);
    }

    public DataColumn addColumn(String columnName) {
        return addColumn(columnName, String.class);
    }


    public DataColumn addColumn(String columnName, Class dataType) {
        DataColumn col = new DataColumn(columnName, dataType);
        if (this.add(col)) {
            return col;
        }
        return null;
    }

    public DataColumn addColumn(int index, String columnName, Class dataType) {
        DataColumn column = new DataColumn(columnName, dataType);
        this.add(index, column);
        return column;
    }

    public boolean contains(String columnName) {
        Objects.requireNonNull(columnName, "columnName is null");
        for (DataColumn column : columns) {
            if (columnName.equals(column.getColumnName())) {
                return true;
            }
        }
        return false;
    }
}
